// PlayerState.java
package org.zjx.core.state.domain;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.zjx.core.state.StateComponent;


/**
 * 玩家领域状态实现
 */
@Data
public class PlayerState implements StateComponent {
    private final String playerId;
    private long version;
    // Getters
    @Getter
    private int hp;
    @Getter
    private int energy;
    @Setter
    @Getter
    private int attackPower;


    private void copyFrom(PlayerState source) {
        this.hp = source.hp;
        this.energy = source.energy;
        this.version = source.version;
    }
    public PlayerState(String playerId) {
        this.playerId = playerId;
        this.version = System.nanoTime();
    }


    @Override
    public void reset() {
        this.version = System.nanoTime();
        this.hp = 100;
        this.energy = 0;
    }

    @Override
    public StateComponent copy() {
        PlayerState copy = new PlayerState(playerId);
        copy.version = this.version;
        copy.hp = this.hp;
        copy.energy = this.energy;
        return copy;
    }

    // 业务方法
    public void takeDamage(int damage) {
        this.hp = Math.max(0, this.hp - damage);
        this.version = System.nanoTime();
    }

    public void gainEnergy(int amount) {
        this.energy += amount;
        this.version = System.nanoTime();
    }

}